package com.itheima.studentsystem;

import java.awt.print.Printable;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作1登录 2注册 3忘记密码 4退出");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1" -> login(list);
                case "2" -> register(list);
                case "3" -> forgetPassword(list);
                case "4" -> System.exit(0);
                default -> System.out.println("没有这个选项，请重新选择");
            }
        }

    }

    private static void forgetPassword(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        int index = getIndex(list, username);
        if(index<0){
            System.out.println("该用户未注册");
            return;
        }
        System.out.println("请输入身份证号码");
        String personId = sc.next();
        System.out.println("请输入手机号码");
        String phoneNumber = sc.next();
        User u = list.get(index);
        if(!(u.getPersonId().equalsIgnoreCase(personId)&&u.getPhoneNumber().equals(phoneNumber))){
            System.out.println("账号信息不匹配，修改失败");
            return;
        }
        while (true) {
            System.out.println("请输入新密码");
            String password1 = sc.next();
            System.out.println("请再次输入新密码");
            String password2 = sc.next();
            if (password1.equals(password2)){
                u.setPassword(password1);
                System.out.println("密码修改成功");
                return;
            }else {
                System.out.println("两次输入的密码不一致，请重新输入");
            }
        }


    }

    private static void register(ArrayList<User> list) {
        //先验证用户名的格式是否满足要求，在验证用户名是否唯一
        Scanner sc = new Scanner(System.in);
        //判断用户名是否满足要求
        String username;
        loop:
        while (true) {
            System.out.println("请输入用户名");
            username = sc.next();
            //判断用户名的长度是否满足要求
            if (!(username.length() >= 3 && username.length() <= 15)) {
                System.out.println("请输入的用户名不满足要求，请重新输入用户名");
                continue;
            }
            //判断用户名是否满足字母加数字的组合
            for (int i = 0; i < username.length(); i++) {
                char c = username.charAt(i);
                if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                    System.out.println("请输入的用户名不满足要求，请重新输入用户名");
                    continue loop;
                }
            }
            //判断用户名是否是纯数字
            int count = 0;
            for (int i = 0; i < username.length(); i++) {
                char c = username.charAt(i);
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                    count++;
                    break;
                }
            }

            if (count <= 0) {
                System.out.println("用户名格式不满足要求，请重新输入用户名");
                continue;
            }
            //判断用户名是否唯一
            boolean flag = contains(list, username);
            if (flag) {
                System.out.println("用户名已存在，请重新输入用户名");
            } else {
                break;
            }
        }
        //输入密码并判断两次密码是否一直
        String password1;
        while (true) {
            System.out.println("请输入用户密码");
            password1 = sc.next();
            System.out.println("请再一次输入用户的密码");
            String password2 = sc.next();
            if (password1.equals(password2)) {
                break;
            } else {
                System.out.println("两次输入的密码不一致请重新输入");
            }
        }
        //判断身份证号码是否满足要求
        String personId;
        loop1:
        while (true) {
            System.out.println("请输入身份证号码");
            personId = sc.next();
            //判断身份证的长度是否满足要求
            if (personId.length() != 18) {
                System.out.println("身份证的长度不满足要求，请重新输入");
                continue;
            }
            //不能以0为开头
            boolean flag = personId.startsWith("0");
            if (flag) {
                System.out.println("身份证的长度不能以0开通，请重新输入");
                continue;
            }
            //前17位，必须都是数字
            for (int i = 0; i < personId.length() - 1; i++) {
                char c = personId.charAt(i);
                if (!(c >= '0' && c <= '9')) {
                    System.out.println("身份证前17位有非数字，请重新输入");
                    continue loop1;
                }
            }
            //最为一位可以是数字，也可以是大写X或小写x
            char c = personId.charAt(personId.length() - 1);
            if (!((c >= '0' && c <= '9') || c == 'x' || c == 'X')) {
                System.out.println("身份证号最后一位不满足要求，请重新输入");
                continue;
            }
            break;

        }
        //手机号验证
        String phoneNumber;
        loop2:
        while (true) {
            System.out.println("请输入手机号码");
            phoneNumber = sc.next();
            //判断手机号长度是否满足要求
            if (phoneNumber.length() != 11) {
                System.out.println("手机号长度不满足要求，请重新输入");
                continue;
            }
            //不能以0开头
            if (phoneNumber.startsWith("0")) {
                System.out.println("手机号不能以0开头，请重新输入");
                continue;
            }
            //是否都是数字
            for (int i = 0; i < phoneNumber.length(); i++) {
                char c = phoneNumber.charAt(i);
                if (!(c >= '0' && c <= '9')) {
                    System.out.println("手机号码中有非数字存在，请重新输入");
                    continue loop2;
                }
            }
            break;
        }
        //走到这说明用户输入的所有信息都满足要求，将这些信息赋给一个新的用户并添加到集合中
        User u = new User(username, password1, personId, phoneNumber);
        list.add(u);
    }

    private static boolean contains(ArrayList<User> list, String username) {
        return getIndex(list, username) >= 0;
    }

    private static int getIndex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getUsername().equals(username)) {
                return i;
            }
        }
        return -1;
    }

    private static void login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        for (int j = 0; j < 3; j++) {
            //键盘录入用户名
            System.out.println("请输入用户名");
            String username = sc.next();
            //判断用户名是否注册
            int indexUser = getIndex(list, username);
            if (indexUser < 0) {
                System.out.println("用户名未注册，请先注册");
                return;
            }
            //键盘录入密码
            System.out.println("请输入密码");
            String password = sc.next();
            //键盘录入验证码
            //判断验证码是否正确
            ArrayList<Character> cList = new ArrayList<>();
            //将小写字母放入集合中
            for (int i = 0; i < 26; i++) {
                char c = (char) ('a' + i);
                cList.add(c);
            }
            //将大写字母放入集合中
            for (int i = 0; i < 26; i++) {
                char c = (char) ('A' + i);
                cList.add(c);
            }
            Random r = new Random();
            while (true) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < 4; i++) {
                    int index = r.nextInt(cList.size());
                    char c = cList.get(index);
                    sb.append(c);
                }

                int i = r.nextInt(10);
                sb.append(i);
                String str = sb.toString();
                char[] arr = str.toCharArray();
                int index = r.nextInt(arr.length);
                char temp = arr[index];
                arr[index] = arr[arr.length - 1];
                arr[arr.length - 1] = temp;
                String newStr = new String(arr);
                System.out.println(newStr);
                System.out.println("请输入验证码");
                String checkNumber = sc.next();
                if (checkNumber.equalsIgnoreCase(newStr)) {
                    StudentSystem ss = new StudentSystem();
                    ss.startStudentSystem();
                    break;
                } else {
                    System.out.println("验证码不正确，请重新输入");
                }

            }


           //判断用户名及密码是否正确
            User u = list.get(indexUser);
            if (u.getUsername().equals(username) && password.equals(u.getPassword())) {
                ;
                break;
            } else {
                System.out.println("用户名或密码错误,你还剩下"+(2-j)+"次机会");
            }
        }


    }

}
